//-----------------------------------------------------------------------
// <copyright file="EmailDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class EmailDAOBase
    {
        #region Common methods
        public virtual Email CreateEmailFromReader(IDataReader reader)
        {
            Email item = new Email();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("MailID"))) item.MailID = (long)reader["MailID"];
                if (!reader.IsDBNull(reader.GetOrdinal("MemberID"))) item.MemberID = (long)reader["MemberID"];
                if (!reader.IsDBNull(reader.GetOrdinal("UserID"))) item.UserID = (long)reader["UserID"];
                if (!reader.IsDBNull(reader.GetOrdinal("SendDate"))) item.SendDate = (DateTime)reader["SendDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("NewsletterId"))) item.NewsletterId = (int)reader["NewsletterId"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateEmailFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateEmail methods
            
        public virtual void CreateEmail(Email email)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spEmailInsert");
                
                database.AddInParameter(dbCommand, "@MemberID", DbType.Int64, email.MemberID);
                database.AddInParameter(dbCommand, "@UserID", DbType.Int64, email.UserID);
                database.AddInParameter(dbCommand, "@SendDate", DbType.DateTime, email.SendDate);
                database.AddInParameter(dbCommand, "@Title", DbType.String, email.Title);
                database.AddInParameter(dbCommand, "@NewsletterId", DbType.Int32, email.NewsletterId);
                database.AddOutParameter(dbCommand, "@MailID", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                email.MailID = (long)database.GetParameterValue(dbCommand, "@MailID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateEmailException, ex);
            }
        }

        #endregion

        #region UpdateEmail methods
        
        public virtual void UpdateEmail(Email email)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spEmailUpdate");            
                
                database.AddInParameter(dbCommand, "@MailID", DbType.Int64, email.MailID);
                database.AddInParameter(dbCommand, "@MemberID", DbType.Int64, email.MemberID);
                database.AddInParameter(dbCommand, "@UserID", DbType.Int64, email.UserID);
                database.AddInParameter(dbCommand, "@SendDate", DbType.DateTime, email.SendDate);
                database.AddInParameter(dbCommand, "@Title", DbType.String, email.Title);
                database.AddInParameter(dbCommand, "@NewsletterId", DbType.Int32, email.NewsletterId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateEmailException, ex);
            }
        }
        
        #endregion

        #region DeleteEmail methods
        public virtual void DeleteEmail(long mailID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spEmailDelete");
                
                database.AddInParameter(dbCommand, "@MailID", DbType.Int64, mailID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteEmailException, ex);
            }
        }

        #endregion

        #region GetEmail methods
        
        public virtual Email GetEmail(long mailID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spEmailGet");
                
                database.AddInParameter(dbCommand, "@MailID", DbType.Int64, mailID);
                
                Email email =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        email = CreateEmailFromReader(reader);
                        reader.Close();
                    }
                }
                return email;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetEmailException, ex);
            }
        }
        
        #endregion

        #region GetEmailList methods
        public virtual EmailCollection GetEmailList(EmailColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spEmailGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                EmailCollection emailCollection = new EmailCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Email email = CreateEmailFromReader(reader);
                        emailCollection.Add(email);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return emailCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetEmailListException, ex);
            }
        }
        
        public virtual EmailCollection GetEmailList(EmailColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetEmailList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetEmailList methods
        public virtual EmailCollection GetEmailListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spEmailGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                EmailCollection emailCollection = new EmailCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Email email = CreateEmailFromReader(reader);
                        emailCollection.Add(email);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return emailCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetEmailListException, ex);
            }
        }
        
        public virtual EmailCollection GetEmailListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetEmailListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}